package com.shiroha.video.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.shiroha.common.feign.client.NotificationClient;
import com.shiroha.video.mapper.CommentMapper;
import com.shiroha.video.pojo.Comment;
import com.shiroha.video.pojo.dto.CommentDTO;
import com.shiroha.video.pojo.vo.CommentVO;
import com.shiroha.video.service.ICommentService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@AllArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    private final NotificationClient notificationClient;

    /**
     * 分页查询作品评论
     * @param userId 用户ID
     * @param workId 作品ID
     * @param pageNum 页数
     * @param pageSize 页面大小
     * @return 分页结果
     */
    public Page<CommentVO> selectCommentByPage(UUID userId, Long workId, int pageNum, int pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("workId", workId);
        params.put("userId", userId);
        Page<CommentVO> commentVOPage = this.mapper.xmlPaginate("selectCommentByPage", Page.of(pageNum, pageSize), params);
        commentVOPage.setRecords(buildCommentTree(commentVOPage.getRecords()));
        return commentVOPage;
    }

    /**
     * 发布或回复评论
     * @param userId 评论者ID
     * @param commentDTO DTO对象
     * @return 发布或回复是否成功
     */
    @Override
    public boolean publishComment(UUID userId, CommentDTO commentDTO) {
        Comment comment = new Comment();
        comment.setWorkId(commentDTO.getWorkId());
        comment.setParentId(commentDTO.getParentId());
        UUID userIdOpt = Optional.ofNullable(userId)
                .orElseGet(commentDTO::getUserId);
        comment.setUserId(userIdOpt);
        comment.setContent(commentDTO.getContent());
        return this.save(comment);
    }

    /**
     * 删除评论
     *
     * @param userId    请求者ID
     * @param commentId 评论ID
     */
    @Override
    public boolean deleteComment(UUID userId, Long commentId) {
        return this.mapper.deleteCommentById(userId, commentId) > 0;
    }

    /**
     * 置顶评论
     *
     * @param userId 请求者ID
     * @param commentId 评论ID
     * @return 置顶是否成功
     */
    @Override
    public boolean pinComment(UUID userId, Long commentId) {
        return this.mapper.pinCommentById(userId, commentId) > 0;
    }


    /**
     * 构建评论树，通过循环调用来设置父子关系
     * @param comments 评论列表
     * @return 构建后的树
     */
    private List<CommentVO> buildCommentTree(List<CommentVO> comments) {
        Map<Long, CommentVO> commentVOMap = new HashMap<>();
        List<CommentVO> rootComments = new ArrayList<>();
        for (CommentVO comment : comments) {
            commentVOMap.put(comment.getId(), comment);
        }

        for (CommentVO comment : comments) {
            if(comment.getParentId() == null) {
                rootComments.add(comment);
            } else {
                CommentVO parent = commentVOMap.get(comment.getParentId());
                if(parent != null) {
                    parent.getChildren().add(comment);
                }
            }
        }
        return rootComments;
    }
}
